package com.healthcloud.utils;

import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JSONObject;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

/**
 * Created by houxianghua on 2015/9/15.
 * 短信
 */
public class Massage {

    /**
     * 发送短信
     * @param mobile 手机号码
     * @param authCode 验证码
     * */
    public static Object eMassage(String mobile, String authCode){
        //发送手机验证码
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String dateTime = sdf.format(new Date());
        String smsUrl = "http://192.168.20.50:8080/Restful/sms/set?sid=1&tophone="+mobile+"&smscontent="+authCode+"&smstype=1&timestamp="+dateTime+"&modelid=26937";
//		String smsUrl = "http://192.168.20.50:8080/Restful/sms/set";
//		JSONObject jobj = new JSONObject();
//		jobj.put("sid", 1);
//		jobj.put("tophone", mobile);
//		jobj.put("smscontent", authCode);
//		jobj.put("smstype", 1);
//		jobj.put("timestamp", dateTime);
//		jobj.put("modelid", 26937);
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(smsUrl);
//		HttpPost httppost = new HttpPost(smsUrl);
        String strResult = "";
        try {
            httpGet.addHeader("Content-type", "application/x-www-form-urlencoded");
            HttpResponse response = httpclient.execute(httpGet);

//			List<NameValuePair> nameValuePairs = new ArrayList<>();
//			nameValuePairs.add(new BasicNameValuePair("msg", getStringFromJson(jobj)));
//			httppost.addHeader("Content-type", "application/x-www-form-urlencoded");
//			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
//			HttpResponse response = httpclient.execute(httppost);

            if (response.getStatusLine().getStatusCode() == 200) {
				/*读返回数据*/
                String conResult = EntityUtils.toString(response.getEntity());
                JSONObject sobj = new JSONObject();
                sobj = sobj.fromObject(conResult);
                String message = sobj.getString("message");
                String smsid = sobj.getString("smsid");
                String code = sobj.getString("code");
                if(code.equals("1000")){
                    strResult += "发送成功";
                    return new ApiRes(true,null,code,strResult);
                }else if(code.equals("1001")){
                    strResult += "无此方法";
                }else if(code.equals("1002")){
                    strResult += "参数数量不足";
                }else if(code.equals("1003")){
                    strResult += "参数值不完整";
                }else if(code.equals("1004")){
                    strResult += "参数值异常";
                }else if(code.equals("1005")){
                    strResult += "信息重复插入";
                }else if(code.equals("1006")){
                    strResult += "发送失败";
                }
                return new ApiRes(false,null,code,strResult);
            } else {
                String err = response.getStatusLine().getStatusCode()+"";
                strResult += "发送请求失败:"+err;
                return new ApiRes(true,null,err,strResult);
            }
//			System.out.println(strResult);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ApiRes(false,null,null,null);
    }

    private static String getStringFromJson(JSONObject adata) {
        StringBuffer sb = new StringBuffer();
        sb.append("{");
        for(Object key:adata.keySet()){
            sb.append("\""+key+"\":\""+adata.get(key)+"\",");
        }
        String rtn = sb.toString().substring(0, sb.toString().length()-1)+"}";
        return rtn;
    }
    /**
     * 发送短信
     * @param mobile 手机号码
     * @param authCode 验证码
     * @param templateNo 模版号
     * */
    public static Object eMassage(String mobile, String content,String templateNo){
        //发送手机验证码
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String dateTime = sdf.format(new Date());
        content = URLEncoder.encode(content);
        String smsUrl = "http://192.168.20.50:8080/Restful/sms/set?sid=1&tophone="+mobile+"&smscontent="+content+"&smstype=1&timestamp="+dateTime+"&modelid="+templateNo;
//		String smsUrl = "http://192.168.20.50:8080/Restful/sms/set";
//		JSONObject jobj = new JSONObject();
//		jobj.put("sid", 1);
//		jobj.put("tophone", mobile);
//		jobj.put("smscontent", authCode);
//		jobj.put("smstype", 1);
//		jobj.put("timestamp", dateTime);
//		jobj.put("modelid", 26937);
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(smsUrl);
//		HttpPost httppost = new HttpPost(smsUrl);
        String strResult = "";
        try {
            httpGet.addHeader("Content-type", "application/x-www-form-urlencoded");
            HttpResponse response = httpclient.execute(httpGet);

//			List<NameValuePair> nameValuePairs = new ArrayList<>();
//			nameValuePairs.add(new BasicNameValuePair("msg", getStringFromJson(jobj)));
//			httppost.addHeader("Content-type", "application/x-www-form-urlencoded");
//			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
//			HttpResponse response = httpclient.execute(httppost);

            if (response.getStatusLine().getStatusCode() == 200) {
				/*读返回数据*/
                String conResult = EntityUtils.toString(response.getEntity());
                JSONObject sobj = new JSONObject();
                sobj = sobj.fromObject(conResult);
                String message = sobj.getString("message");
                String smsid = sobj.getString("smsid");
                String code = sobj.getString("code");
                if(code.equals("1000")){
                    strResult += "发送成功";
                    return new ApiRes(true,null,code,strResult);
                }else if(code.equals("1001")){
                    strResult += "无此方法";
                }else if(code.equals("1002")){
                    strResult += "参数数量不足";
                }else if(code.equals("1003")){
                    strResult += "参数值不完整";
                }else if(code.equals("1004")){
                    strResult += "参数值异常";
                }else if(code.equals("1005")){
                    strResult += "信息重复插入";
                }else if(code.equals("1006")){
                    strResult += "发送失败";
                }
                return new ApiRes(false,null,code,strResult);
            } else {
                String err = response.getStatusLine().getStatusCode()+"";
                strResult += "发送请求失败:"+err;
                return new ApiRes(true,null,err,strResult);
            }
//			System.out.println(strResult);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ApiRes(false,null,null,null);
    }
}
